// 加载菜单列表
let MENU_LIST = (state, payload) => {
    state.menu = [...payload];
    // 要浅拷贝一下，防止出现其他问题
}

// 添加菜单
let ADD_MENU = (state, new_menu) => {
    state.menu.push(new_menu);
}

// 添加操作按钮
let ADD_OPERATION = (state, new_operate) => {
    console.log(new_operate);
    state.menu.push(new_operate);
}

// 编辑菜单
let UPDATE_MENU = (state, payload) => {
    let index = state.menu.findIndex(menu => menu.menu_id === payload.menu_id);
    if (index !== -1) {
        // 使用 splice 方法更新数组，以便触发响应式更新机制
        state.menu.splice(index, 1, payload);
    }
}

// 编辑菜单
let UPDATE_OPERATE = (state, payload) => {
    let index = state.menu.findIndex(menu => menu.permission_id === payload.permission_id);
    if (index !== -1) {
        // 使用 splice 方法更新数组，以便触发响应式更新机制
        state.menu.splice(index, 1, payload);
    }
}

// 删除菜单
let REMOVE_MENU = (state, permission_id) => {
    let index = state.menu.findIndex(item => item.permission_id === permission_id);
    if (index !== -1) {
        state.menu.splice(index, 1);
    }
}

// 删除操作按钮
let REMOVE_OPERATION = (state, permission_id) => {
    let index = state.menu.findIndex(item => item.permission_id === permission_id);
    if (index !== -1) {
        state.menu.splice(index, 1);
    }
}

// 更改图标
let UPDATE_ICON = (state, payload) => {
    let { icon: { icon_name: newIcon }, menu: { icon_name: oldIcon, menu_id } } = payload;
    let new_name = state.menu.find(item => item.menu_id === menu_id);
    new_name.icon_name = newIcon;
};

// 根据角色id，
let MENU_POWER = (state, payload) => {
    state.menu_power_list = [...payload];
    // 要浅拷贝一下，防止出现其他问题
}


export default {
    MENU_LIST,
    ADD_MENU,
    UPDATE_MENU,
    ADD_OPERATION,
    UPDATE_OPERATE,
    REMOVE_MENU,
    REMOVE_OPERATION,
    UPDATE_ICON,
    MENU_POWER,
}